#!/usr/bin/perl
#vol_delete.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";
my $scsi_dev;
my $volname = $in{'volume'};
$pvldname = "Unit".$volname;
&foreign_require("lvm", "lvm-lib.pl");
my @pvs = &foreign_call("lvm", "get_pv_info");
foreach $p (@pvs) {
	if(($p->{'raid'} eq $pvldname) && ($p->{'vg'} ne '')){
		$err_msg=$text{'err_raid_in_vg'};
		goto ERROR_OUT;
	}
	if($p->{'raid'} =~ /$pvldname/) {
		$scsi_dev = $p->{'pv'};
	}
}
#if (!$volname) {
#	$err_msg = $text{"err_cgiparam"};
#	goto ERROR_OUT;
#}
my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
#	goto ERROR_OUT;
}
my (@rdinfo, $r, $rr, @lvinfo);
@rdinfo = get_mega_raid_info();
$found = 0;
foreach $r (@rdinfo) {
	if ($r->{'id'} eq $volname) {
		$rr = $r;
		$found = 1;
		last;
	}
}
if (!$found) {
	$err_msg = $text{"err_configchanged"};
#	goto ERROR_OUT;
}

my ($out, $mddev, @rdev);

$volume_name = $rr->{'volname'};

my ($d, $p);
#check whether the status of volume

$err_msg = $rr->{'name'}.$pvldname.$scsi_dev.$rr->{'status'};
#goto ERROR_OUT;
if ($rr->{'status'} eq 'new') {
	#raid stop
}
elsif (($rr->{'status'} eq 'Optimal') || ($rr->{'status'} =~ /Degraded/) || ($rr->{'status' =~ /Offline/})) {
				`/sbin/lvm pvremove $scsi_dev -f -y`;
        $uid = "L".$rr->{'name'};
        system("/sbin/MegaCli -ldcc -abort -$uid -a0");
        system("/sbin/MegaCli -ldbi -abort -$uid -a0"); 
        system("/sbin/MegaCli  -cfglddel -$uid -a0");
	#system("/bin/rm $scsi_dev");
        if(!$?){
        }
}
else {  
	$err_msg = $rr->{'status'}; #$text{"err_configchanged"};
	goto ERROR_OUT;	
}

$log_ev = $text{'log_ev_delvolume_ok'};
$log_msg= &text('log_msg_delvolume_ok', $volname);
nas_syslog($text{'my_log_minor'}, 'naslog_info', $log_ev, $log_msg);
#`/sbin/reboot`;
redirect("index.cgi");

ERROR_OUT:
&error_setup($text{'del_err_title'});
&error($err_msg);
